﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_support_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
       // this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
        if (!this.IsPostBack) {
            InitItem();
        }
    }
   
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }

    private void AddNew()
    {
        String strMess = "";

        try
        {
            int ID=-1;
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
            WebData db = new WebData();
            DataLibrary.tbl_Weblink item = new DataLibrary.tbl_Weblink();
            item.Title = txtTitle.Text;
            item.Url = txtUrl.Text;
            item.IdWeblink = ID;
            item.IsActive = chkisActive.Checked == true ? 1 : 0;
            int i = db.tbl_Weblinks.UpdateOnSubmit(item);
            strMess = i == -1 ? Message.Update_err : Message.Update;

        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);

    }


    private void InitItem()
    {
        int ID=-1;
        try {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        WebData db=new WebData();
        DataLibrary.tbl_Weblink item = db.tbl_Weblinks.GetItemById(ID);
        if (item != null) {
            txtTitle.Text = item.Title;
            txtUrl.Text = item.Url;
            chkisActive.Checked = item.IsActive == 1 ? true : false;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}